package com.xinruke.hostels.common.mybatis;

import com.xinruke.hostels.common.vo.query.Condition;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.OrderField;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

/**
 * @ClassName MyExample.java
 * @CreateDate 2019/6/8
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 自定义Example
 */
public class MyExample extends Weekend {
    //TODO 查询中，增加当前酒店的查询条件
    public MyExample(Class<?> entityClass) {
        super(entityClass);
    }

    public MyExample(Class<?> entityClass, QueryConditions queryConditions) {
        super(entityClass);
        dealCondition(queryConditions);
    }

    public MyExample(Class<?> entityClass, QueryConditions queryConditions, List<OrderField> orderFieldList) {
        super(entityClass);
        orderby(orderFieldList);
        dealCondition(queryConditions);
    }


    public Weekend createOrWeekend(Class<?> entityClass, QueryConditions queryConditions) {
        Weekend weekend = new Weekend(entityClass);
        weekend.setOrderByClause(this.getOrderByClause());
        WeekendCriteria weekendCriteria = weekend.weekendCriteria();

        List<Condition> conditionList = queryConditions.getConditionList();
        if (CollectionUtils.isEmpty(conditionList)) {
            return weekend;
        }
        for (Condition condition : conditionList) {
            if (EConditionOperator.BETWEEN == condition.getOperator()) {
                if (StringUtils.isEmpty(condition.getValueStart()) || StringUtils.isEmpty(condition.getValueEnd())) {
                    continue;
                }
                weekendCriteria.orBetween(condition.getField(), condition.getValueStart(), condition.getValueEnd());
            } else {
                if (StringUtils.isEmpty(condition.getValue())) {
                    continue;
                }
                switch (condition.getOperator()) {
                    case EQULS:
                        weekendCriteria.orEqualTo(condition.getField(), condition.getValue());
                        break;
                    case LIKE:
                        weekendCriteria.orLike(condition.getField(), "%" + condition.getValue() + "%");
                    default:
                        break;
                }
            }
        }

        return weekend;
    }

    private void dealCondition(QueryConditions queryConditions) {
        Criteria criteria = this.createCriteria();

        List<Condition> conditionList = queryConditions.getConditionList();
        if (conditionList != null && conditionList.size() > 0) {
            for (Condition condition : conditionList) {

                if (EConditionOperator.BETWEEN == condition.getOperator()) {
                    if (StringUtils.isEmpty(condition.getValueStart()) || StringUtils.isEmpty(condition.getValueEnd())) {
                        continue;
                    }
                } else if (EConditionOperator.IN == condition.getOperator()) {
                    if (CollectionUtils.isEmpty(condition.getValueList())) {
                        continue;
                    }
                } else {
                    if (StringUtils.isEmpty(condition.getValue())) {
                        continue;
                    }
                }
                if (EConditionOperator.EQULS == condition.getOperator()) {
                    criteria.andEqualTo(condition.getField(), condition.getValue());
                } else if (EConditionOperator.LIKE == condition.getOperator()) {
                    criteria.andLike(condition.getField(), "%" + condition.getValue() + "%");
                } else if (EConditionOperator.GREATER == condition.getOperator()) {
                    criteria.andGreaterThanOrEqualTo(condition.getField(), condition.getValue());
                } else if (EConditionOperator.LESS == condition.getOperator()) {
                    criteria.andLessThanOrEqualTo(condition.getField(), condition.getValue());
                } else if (EConditionOperator.BETWEEN == condition.getOperator()) {
                    criteria.andBetween(condition.getField(), condition.getValueStart(), condition.getValueEnd());
                } else if (EConditionOperator.IN == condition.getOperator()) {
                    criteria.andIn(condition.getField(), condition.getValueList());
                }

            }
        }
    }

    /**
     * @param orderFieldList
     */
    private void orderby(List<OrderField> orderFieldList) {
        if (orderFieldList != null && orderFieldList.size() > 0) {
            for (OrderField orderField : orderFieldList) {
                String field = orderField.getOrderField();
                if (StringUtils.isEmpty(field)) {
                    continue;
                }
                if (orderField.isOrderDesc()) {
                    this.orderBy(orderField.getOrderField()).desc();
                } else {
                    this.orderBy(orderField.getOrderField()).asc();
                }
            }
        }
    }

}
